<?php

header("Access-Control-Allow-Origin:*");
/*
  参数:
  cid //课时名称
  uid //学生编号
 */
require_once '../../dz_framework/init.php';
$json_data = $_POST;
//$json_data = '{"cid":"123","uid":"14","hid":"4","totalScore":54,"questions":[{"tid":"9","choice":"0","daan":"0"},{"tid":"7","daan":"1","choice":"2"},{"tid":"10","choice":"2,3","daan":"3,4,5"},{"tid":"2","choice":-1,"daan":"2,3"},{"tid":"6","choice":-1,"daan":"2,3"},{"tid":"4","choice":"1","daan":"0"}]}';
//print_r($json_data);
//$json_data = json_decode($json_data,true);
//$aa = 'a:7:{i:9;a:3:{s:3:"tid";s:1:"9";s:6:"choice";s:1:"2";s:4:"daan";s:1:"0";}i:1;a:3:{s:3:"tid";s:1:"1";s:6:"choice";s:1:"3";s:4:"daan";s:1:"2";}i:7;a:3:{s:3:"tid";s:1:"7";s:6:"choice";s:2:"-1";s:4:"daan";s:1:"1";}i:10;a:3:{s:3:"tid";s:2:"10";s:6:"choice";s:2:"-1";s:4:"daan";s:5:"3,4,5";}i:2;a:3:{s:3:"tid";s:1:"2";s:6:"choice";s:2:"-1";s:4:"daan";s:3:"2,3";}i:6;a:3:{s:3:"tid";s:1:"6";s:6:"choice";s:2:"-1";s:4:"daan";s:3:"2,3";}i:4;a:3:{s:3:"tid";s:1:"4";s:6:"choice";s:2:"-1";s:4:"daan";s:1:"0";}}';
//
//print_r(unserialize($aa));
$getdaan = $json_data['questions'];

foreach ($getdaan as $key => $value) {
    $temp_daan[$value['tid']] = $value;
}
if (is_array($temp_daan)) {
    $student_exam_info = DB::fetch_first("select * from " . DB::table('student_practice') . " where user_id='" . $json_data['uid'] . "' and h_id='" . $json_data['hid'] . "'");
    $teacher_id = DB::result_first("select user_id from " . DB::table('teacher_exam_homework') . " where h_id='" . $json_data['hid'] . "'");
    $insert_data = array('teacher_id' => $teacher_id,
        'user_id' => $json_data['uid'],
        'h_id' => $json_data['hid'],
        'daan' => serialize($temp_daan),
        'comment' => $json_data['comment'],
        'time' => TIMESTAMP
    );

    if ($student_exam_info) {
        DB::update('student_practice', array('daan' => serialize($temp_daan)), array('h_id' => $json_data['hid'], 'user_id' => $json_data['uid']));
        echo 2;
    } else {
        DB::insert('student_practice', $insert_data);
        echo 1;
    }
}
//$array = array('0' => "Eric", '1' => array('test'=>111,'ex'=>'test'));   echo json_encode($array);
